package com.spdg.ring.datamgr;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import cn.wolf.base.DataManager;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.sqlite.model.ComparatorArticleList;
import com.spdg.ring.sqlite.service.ArticleListService;
import com.spdg.ring.sqlite.service.ArticleService;

/**
 * <p>Title:  文章列表，文章数据管理类</p>
 * <p>Description:  </p>
 * <p>@author: caijj                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-11             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleDataMgr extends DataManager {

	private static ArticleDataMgr mInstance = new ArticleDataMgr();

	public static ArticleDataMgr getInstance() {
		if (mInstance == null)
			mInstance = new ArticleDataMgr();
		return mInstance;
	}

	private Map<String, List<ArticleList>> articles = new HashMap<String, List<ArticleList>>();
	private boolean LOAD_SUCCESS = false;

	public synchronized void loadDataSqlite(Context context) {
		ArticleListService service = ArticleListService.newInstance(context);
		List<ArticleList> list = service.findAll();

		articles.clear();

		if (list != null && list.size() > 0) {
			ComparatorArticleList comparator = new ComparatorArticleList();
			Collections.sort(list, comparator);

			String fileKey = null;
			List<ArticleList> keyList = new ArrayList<ArticleList>();
			// 把parentFile相同的数据，保存到一起
			for (int i = 0; i < list.size(); i++) {
				ArticleList article = list.get(i);
				String parentFile = article.getParentFile();

				if (i == 0) { // 第一次赋值
					fileKey = parentFile;
				}

				// key不相同，保存数据
				if (!parentFile.equals(fileKey)) {
					articles.put(fileKey, keyList);
					keyList = new ArrayList<ArticleList>();
					fileKey = parentFile;
				}

				if ((i + 1) == list.size()) {
					articles.put(fileKey, keyList);
					keyList = new ArrayList<ArticleList>();
				}

				keyList.add(article);
			}
		}

		LOAD_SUCCESS = true;
	}

	/**
	 * 从数据库加载数据，保存到缓存
	 * @param context
	 */
	public void loadDataSqlite(Context context, String fileKey) {
		ArticleListService service = ArticleListService.newInstance(context);
		List<ArticleList> list = service.findByParentKey(fileKey);

		if (list != null && list.size() > 0) {
			refresh(context, fileKey, list);
		}
	}

	/**
	 * 保存数据到数据库
	 * @param context
	 * @param list
	 */
	public void saveDataSqlite(Context context, String parentKey, List<ArticleList> list) {
		if (list == null || list.size() <= 0) {
			return;
		}

		ArticleListService service = ArticleListService.newInstance(context);
		for (ArticleList article : list) {
			article.setParentFile(parentKey);
			service.saveOrUpdate(article);
		}
	}

	/**
	 * 刷新文章列表数据
	 * 1、缓存不存在，保存数据
	 * 2、已缓存，清空缓存数据，重新缓存
	 * @param context
	 * @param fileKey
	 * @param data
	 */
	public void refresh(Context context, String fileKey, List<ArticleList> data) {
		if (StringUtil.isEmpty(fileKey)) {
			Log.i("the fileKey is null, HashMap need a key...");
			return;
		}

		// 不存在，直接缓存
		if (!articles.containsKey(fileKey)) {
			articles.put(fileKey, data);
		} else {
			// 清除原数据，缓存新数据
			List<ArticleList> list = articles.get(fileKey);
			if (list != null) {
				list.clear();
				list = null;
			}
			articles.put(fileKey, data);
		}

	}

	public List<ArticleList> getArticleList(Context context, String fileKey) {

		List<ArticleList> list = new ArrayList<ArticleList>();
		if (StringUtil.isEmpty(fileKey)) {
			return list;
		}

		if (!LOAD_SUCCESS) {
			loadDataSqlite(context);
		}

		if (articles.containsKey(fileKey)) {
			list = articles.get(fileKey);
		}

		return list;
	}

	public void clearSqliteData(Context context) {
		ArticleListService service = ArticleListService.newInstance(context);
		service.removeAll();

		ArticleService articleService = ArticleService.newInstance(context);
		articleService.removeAll();
	}
}
